package cn.itsource.ymcc.mq;

import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.result.JSONResult;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MediaProducer {

    @Autowired
    private RocketMQTemplate mqTemplate;

    public JSONResult synSend(MediaFile mediaFile){
        // 把文件信息 发送到mq
        String str = JSON.toJSONString(mediaFile);
        log.info(str);
        SendResult sendResult = mqTemplate.syncSend("topic-media:tags-course", MessageBuilder.withPayload(str).build());

        if(sendResult.getSendStatus().equals(SendStatus.SEND_OK)){
            return JSONResult.success();
        }else {
            return JSONResult.error("发送MQ消息失败");
        }


    }
}
